package com.javasm.mb_store_api_final.sys_control.usercontrol.sys_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.mb_store_api_final.sys_control.rolecontrol.sys_role.dao.SysRoleDao;
import com.javasm.mb_store_api_final.sys_control.rolecontrol.sys_role.entity.SysRole;
import com.javasm.mb_store_api_final.sys_control.usercontrol.authority.entity.Authority;
import com.javasm.mb_store_api_final.sys_control.usercontrol.authority.service.AuthorityService;
import com.javasm.mb_store_api_final.sys_control.usercontrol.sys_menu.dao.SysMenuDao;
import com.javasm.mb_store_api_final.sys_control.usercontrol.sys_menu.entity.SysMenu;
import com.javasm.mb_store_api_final.sys_control.usercontrol.sys_menu.service.SysMenuService;
import com.javasm.mb_store_api_final.sys_control.usercontrol.sys_user.entity.SysUser;
import com.javasm.mb_store_api_final.sys_control.usercontrol.sys_user.dao.SysUserDao;
import com.javasm.mb_store_api_final.sys_control.usercontrol.sys_user.service.SysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * (SysUser)表服务实现类
 *
 * @author makejava
 * @since 2024-07-29 20:19:15
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao,SysUser> implements SysUserService {
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private AuthorityService authorityService;
    @Resource
    private SysRoleDao sysRoleDao;

    @Override
    public List<SysUser> queryAll() {
       List<SysUser> list = sysUserDao.selectAll();
       return list;
    }

    @Override
    public List<SysUser> queryBy(SysUser user) {
     List<SysUser> list = sysUserDao.selectListWithDepart(user);
      return list;
    }

    @Override
    public void add(SysUser user) {
        sysUserDao.add(user);
    }

    @Override
    public void update(Integer userId, SysUser user) {
        sysUserDao.updateUser(userId,user);
    }

    @Override
    public void deleteById(Integer userId) {
        sysUserDao.deleteByUid(userId);
    }

    private SysUser setDetails(SysUser sysUser){
        Integer rid = sysUser.getRoleId();
        SysRole sysRole = sysRoleDao.selectById(rid);
        sysUser.setRoleName(sysRole.getRoleName());
        List<SysMenu> menus=sysMenuService.listMenuByRid(rid);
        sysUser.setMenus(menus);
        String posId = sysUser.getPosId();
        List<Authority> authorities = authorityService.listAuthorityByPosId(posId);
        sysUser.setAuthorities(authorities);
        return sysUser;
    }
    @Override
    public SysUser getByPhone(String phone) {

        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUserPhone,phone);
        SysUser one = getOne(queryWrapper);
        System.out.println(one);
        one=setDetails(one);
        return one;
    }

    @Override
    public SysUser getByUsername(String name) {

        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUserName,name);
        SysUser one = getOne(queryWrapper);
        System.out.println(one);
        one=setDetails(one);
        return one;
    }

    @Override
    public SysRole getRole(int roleId) {

            SysRole sysRole = sysRoleDao.selectById(roleId);
            return sysRole;

    }

    @Override
    public Map<String, Set<String>> paramList() {
        // 1. 查询所有用户
        List<SysUser> users = sysUserDao.selectAll();

        // 使用流处理来分别提取所需属性到对应的集合中
        Map<String, Set<String>> attributes = new HashMap<>();

        attributes.put("departName", users.stream().map(SysUser::getDepartName).collect(Collectors.toSet()));
        attributes.put("userState", users.stream().map(user -> String.valueOf(user.getUserState())).collect(Collectors.toSet()));
        attributes.put("posName", users.stream().map(SysUser::getPosName).collect(Collectors.toSet()));
        attributes.put("roleName", users.stream().map(SysUser::getRoleName).collect(Collectors.toSet()));
        attributes.put("userGender",users.stream().map(user -> String.valueOf(user.getUserGender())).collect(Collectors.toSet()));
        // 返回Map对象
        return attributes;
    }

    @Override
    public long countall() {
        Long count = sysUserDao.selectAllCount();
        return count;
    }
}
